package br.com.almoxarifadoporttek.persistence;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import br.com.almoxarifadoporttek.util.HibernateUtil;

import br.com.almoxarifadoporttek.model.Componente;
import br.com.almoxarifadoporttek.model.Historico;
import br.com.almoxarifadoporttek.model.Usuario;

public class HistoricoDAO implements IHistoricoDAO{

	@Override
	public void save(Historico historico) 
	{
		Session session = null;
		try
		{
			session= HibernateUtil.getSession().openSession();
		
			Transaction t = session.beginTransaction();
			session.save(historico);
			t.commit();
			
		}catch(Exception erro){			
			try
			{			
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();				
				}				
			} catch (Exception e) {
				
			}		
		}		
		finally 
		{
			session.close();	
		}
		
	}

	@Override
	public List<Historico> list() 
	{

		Session session = null;
		
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			//List lista = session.createQuery("from Livro").list();		
			 
			List<Historico> lista = session.createCriteria(Historico.class).list();
			
			for(Historico hist : lista)
			{
				hist.getUsuario().getId();
				hist.getUsuario().getEmail();
				hist.getUsuario().getNivel().getNivel();
				hist.getUsuario().getNivel().getId();
				hist.getUsuario().getNome();
				hist.getUsuario().getSenha();
				hist.getUsuario().getUsuario();
				
				hist.getComponente().getId();
				hist.getComponente().getDescricao();
				hist.getComponente().getNome();
				hist.getComponente().getFabricante();
				hist.getComponente().getQuantidade();
				hist.getComponente().getLocal().getId();
				hist.getComponente().getLocal().getCodigo();
				
			}
			
			t.commit();
			return lista;
			
		}
		catch(Exception erro)
		{
			
			try
			{			
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
				}
			
				
			}
				catch (Exception e) 
				{}
			
			return null;			
		}
		
		finally 
		{
			session.close();		
		}	
	}

	@Override
	public void delete(Historico historico) 
	{
		Session session = null;
		try
		{
			session= HibernateUtil.getSession().openSession();
		
			Transaction t = session.beginTransaction();
			session.delete(historico);
			t.commit();
			
		}
		catch(Exception erro)
		{			
			try
			{			
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();				
				}				
			} 
			catch (Exception e) 
			{}		
		}		
		finally 
		{
			session.close();	
		}
		
	}

	@Override
	public void update(Historico historico) 
	{
		Session session = null;
		try
		{
			session= HibernateUtil.getSession().openSession();
		
			Transaction t = session.beginTransaction();
			session.update(historico);
			t.commit();
			
		}
		catch(Exception erro)
		{			
			try
			{			
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();				
				}				
			} 
			catch (Exception e) 
			{}		
		}		
		finally 
		{
			session.close();	
		}
		
	}

	@Override
	public List<Historico> listByComponenteAndData(Componente componente,
			Date dataInicial, Date dataFinal) {
			
		Session session = null;
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			Criteria criteria = session.createCriteria(Historico.class);
			criteria.add(Restrictions.eq("componente", componente));
			criteria.add(Restrictions.between("dataAcao", dataInicial, dataFinal));
			List<Historico> lista = criteria.list();
		
			for(Historico hist : lista)
			{
				hist.getUsuario().getId();
				hist.getUsuario().getEmail();
				hist.getUsuario().getNivel().getNivel();
				hist.getUsuario().getNivel().getId();
				hist.getUsuario().getNome();
				hist.getUsuario().getSenha();
				hist.getUsuario().getUsuario();
				
				hist.getComponente().getId();
				hist.getComponente().getDescricao();
				hist.getComponente().getNome();
				hist.getComponente().getFabricante();
				hist.getComponente().getQuantidade();
				hist.getComponente().getLocal().getId();
				hist.getComponente().getLocal().getCodigo();
				
			}
			t.commit();
			return lista;
			
		}
		catch(Exception erro)
		{			
			
			try
			{			
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();				
				}				
			} 
			catch (Exception e) 
			{}	
			
			return null;
		}		
		finally 
		{
			session.close();	
		}
		
		
	}

	@Override
	public List<Historico> listByUsuarioAndData(Usuario usuario,
			Date dataInicial, Date dataFinal) {
		
		Session session = null;
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			Criteria criteria = session.createCriteria(Historico.class);
			criteria.add(Restrictions.eq("usuario", usuario));
			criteria.add(Restrictions.between("dataAcao", dataInicial, dataFinal));
			List<Historico> lista = criteria.list();
		
			for(Historico hist : lista)
			{
				hist.getUsuario().getId();
				hist.getUsuario().getEmail();
				hist.getUsuario().getNivel().getNivel();
				hist.getUsuario().getNivel().getId();
				hist.getUsuario().getNome();
				hist.getUsuario().getSenha();
				hist.getUsuario().getUsuario();
				
				hist.getComponente().getId();
				hist.getComponente().getDescricao();
				hist.getComponente().getNome();
				hist.getComponente().getFabricante();
				hist.getComponente().getQuantidade();
				hist.getComponente().getLocal().getId();
				hist.getComponente().getLocal().getCodigo();
				
			}
			t.commit();
			return lista;
			
		}
		catch(Exception erro)
		{			
			
			try
			{			
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();				
				}				
			} 
			catch (Exception e) 
			{}	
			
			return null;
		}		
		finally 
		{
			session.close();	
		}
		
		
	}

}
